package com.kede.pay.chain;

import com.alibaba.fastjson.JSONObject;
import com.kede.api.ienum.VipEnum;
import com.kede.pay.annotation.PayNotifyHandler;
import com.kede.pay.domain.UsersDomain;
import com.kede.pay.enumstatus.vip.VipActionEnum;
import com.kede.pay.pojo.NotifyPOJO;
import com.kede.pay.service.ConfigService;
import com.kede.pay.service.UsersService;

import javax.annotation.Resource;

/**
 * @Author:Pr.li
 * 置换数不限制
 * 但是只能去特定商城消费
 * @Date: create in 2023/6/25 23:45
 */
@PayNotifyHandler("Vp1HandlerNotify")
public class Vp1HandlerNotifyImpl implements IHandlerNotify{

    @Resource
    private UsersService usersService;

    @Resource
    private ConfigService configService;


    @Override
    public boolean match(String tag) {
        return tag.toLowerCase().equals(VipEnum.Vip1.getCode().toLowerCase());
    }

    @Override
    public void doHandler(NotifyPOJO notifyPOJO) {
        JSONObject jsonObject = JSONObject.parseObject(notifyPOJO.getAttach());
        Integer userid = jsonObject.getInteger("userid");
        Integer action = jsonObject.getInteger("action");
        UsersDomain user = usersService.getById(userid);
        if(notifyPOJO.getTrade_state().equals("SUCCESS")){
            if (user==null){
                return;
            }
            if (action.equals(VipActionEnum.RENEWAL.getCode())){
                Integer extendExpiretime = configService.extendExpiretime(user.getExpireTime());
                user.setExpireTime(extendExpiretime);
            }
            if (action.equals(VipActionEnum.OPEN.getCode())){
                Integer expiretime = configService.getExpiretime();
                user.setExpireTime(expiretime);
            }
            user.setIsVip(VipEnum.Vip1.getValue());
            usersService.updateById(user);
            //支付成功
        }
    }
}
